package com.tgy.leetcode;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class _1047_删除字符串中的所有相邻重复项 {

    public static String removeDuplicates(String S) {

        char[] chars = S.toCharArray();
        Deque<Character> deque = new LinkedList<>();
        for (char aChar : chars) {


            boolean flag = true;
            while (!deque.isEmpty() && deque.peek() == aChar) {

                deque.pop();
                flag = false;
            }

            if (flag) {
                deque.push(aChar);
            }
        }

        StringBuilder sb = new StringBuilder();

        while (!deque.isEmpty()) {

            sb.append(deque.removeLast());
        }

        return sb.toString();
    }

    public static void main(String[] args) {

        String duplicates = removeDuplicates("abbaca");
        System.out.println(duplicates);
    }

}
